/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.popup;

import com.daikit.commons.shared.bean.AbstractDkBeanWithId;
import com.daikit.daikit4gxt.client.action.BaseAction;
import com.daikit.daikit4gxt.client.action.BaseAsyncCallback;
import com.sencha.gxt.data.shared.ListStore;


public abstract class MyYesNoForDeleteMessageBox<B extends AbstractDkBeanWithId> extends MyYesNoMessageBox
{

	protected B modelToDelete;
	private ListStore<B> store;

	public MyYesNoForDeleteMessageBox()
	{
		super(null, null, MessageType.WARNING);
		setHeadingText(getLabelConfirmationTitle());
		setMessage(getLabelConfirmationMessage());
	}

	public void show(final ListStore<B> store, final B modelToDelete)
	{
		this.modelToDelete = modelToDelete;
		this.store = store;
		super.show();
	}

	public B getModelToDelete()
	{
		return modelToDelete;
	}

	@Override
	public void processYes()
	{
		getActionDelete().execute();
	}

	protected BaseAction<?> getActionDelete()
	{
		return new BaseAction<Void>(getLabelActionDeleteLoadingLabel())
		{
			@Override
			protected void run()
			{
				callDeleteRpc(modelToDelete, new BaseAsyncCallback<Void>(this, true)
				{
					@Override
					public void processSuccess(final Void result)
					{
						MyMessageBox.info(getLabelPopupDeleteOkMessageBoxTitle(), getLabelPopupDeleteOkMessageBoxMessage(),
								new AfterCloseListener()
								{
									@Override
									public void afterClose()
									{
										getStore().remove(modelToDelete);
									}
								});
					}
				});
			}
		};
	}

	protected ListStore<B> getStore()
	{
		return this.store;
	}

	/**
	 * Please use {@link #show(AbstractDkBeanWithId)} instead of show()
	 */
	@Deprecated
	@Override
	public void show()
	{
		throw new RuntimeException("Please use show(M modelToDelete) instead of show()");
	}

	protected abstract String getLabelActionDeleteLoadingLabel();

	protected abstract String getLabelConfirmationTitle();

	protected abstract String getLabelConfirmationMessage();

	protected abstract String getLabelPopupDeleteOkMessageBoxTitle();

	protected abstract String getLabelPopupDeleteOkMessageBoxMessage();

	protected abstract void callDeleteRpc(B bean, BaseAsyncCallback<Void> callback);

}
